package com.sdkj.dspservice.common.shiro;

import com.sdkj.dspservice.modules.sys.entity.Menu;
import com.sdkj.dspservice.modules.sys.entity.User;
import com.sdkj.dspservice.modules.sys.service.MenuService;
import com.sdkj.dspservice.modules.sys.service.UserService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.credential.CredentialsMatcher;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.List;

public class CustomRealm extends AuthorizingRealm {

    @Autowired
    private UserService userService;
    @Autowired
    private MenuService menuService;

    /**
     * @MethodName doGetAuthorizationInfo
     * @Description 权限配置类
     * @Param [principalCollection]
     * @Return AuthorizationInfo
     * @Author WangShiLin
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        // 获取账号
        String loginName = (String) principalCollection.getPrimaryPrincipal();
        // 通过账号获取用户
        User user = userService.getByLoginName(loginName);
        // 添加角色和权限
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
//        if(){
//
//        }
//        for (Role role : user.getRoles()) {
            //添加角色
            simpleAuthorizationInfo.addRole("test");
            //添加权限
        // TODO: 2021/2/24 暂时添加上所有权限 
        List<Menu> menuList = menuService.list();
            for (Menu menu : menuList) {
                simpleAuthorizationInfo.addStringPermission(menu.getPermission() + ":view");
                simpleAuthorizationInfo.addStringPermission(menu.getPermission() + ":edit");
            }
//        }
        return simpleAuthorizationInfo;
    }

    /**
     * @MethodName doGetAuthenticationInfo
     * @Description 认证配置类
     * @Param [authenticationToken]
     * @Return AuthenticationInfo
     * @Author WangShiLin
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        if (StringUtils.isEmpty(authenticationToken.getPrincipal())) {
            return null;
        }
        // 获取用户信息
        String loginName = authenticationToken.getPrincipal().toString();
        User user = userService.getByLoginName(loginName);
        if (user == null) {
            // 这里返回后会报出对应异常
            return null;
        } else {
            //这里验证authenticationToken和simpleAuthenticationInfo的信息
            SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(loginName, user.getPassword(), getName());
            return simpleAuthenticationInfo;
        }
    }

    /**
     * 设置认证加密方式
     */
    @Override
    public void setCredentialsMatcher(CredentialsMatcher credentialsMatcher) {
        CustomCredentialsMatcher customCredentialsMatcher = new
                CustomCredentialsMatcher();
        super.setCredentialsMatcher(customCredentialsMatcher);
    }
}

